package Screens;

import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import resource.HighScoreStore;
import resource.HighScoreScreen;
import resource.Keyboard;

import Game.CanvasGame;
import Objects.Statistic;

public class HighScore extends Screen {
	private Vector v;
	private Image[] img;
	public HighScore(CanvasGame _canvas) {
		super(_canvas);
		HighScoreStore p = new HighScoreStore();
		p.open();
		v = p.read();
		p.close();
		p = null;
		img = new Image[HighScoreScreen.IMG_LENGHT];
		load_screen();
		// TODO Auto-generated constructor stub
	}

	public void load_screen() {
		// TODO Auto-generated method stub
		for (int i = 0; i < HighScoreScreen.IMG_LENGHT; i++) {
			load(i);
		}
	}
	
	public void un_load_screen() {
		// TODO Auto-generated method stub
		//img = null;
		if(img == null)    return;
        for (int i = 0; i < HighScoreScreen.IMG_LENGHT; i++) {
            unLoad(i);
        }
	}


	public boolean load(int i) {
		/* TODO: load the images here */
	    try {
	    	img[i] = Image.createImage(HighScoreScreen.imgSource[i]);
	    } catch (Exception ex) {
	    	System.err.println(ex.toString()+"aaaa");
	    	return false;
	    }
	    return true;
	}
	
	public void unLoad(int i) {
        /* TODO: make sure the object get's destroyed */
        img[i] = null;
    }

	public void keyEvent(int keycode, int event) {
		// TODO Auto-generated method stub
		if(event == Keyboard.KEY_EVENT_DOWN){
            switch(keycode){
                case Keyboard.KEY_OK:
                	un_load_screen();
                	canvas.setScreen(new Menu(canvas));
                    break;
                default:
                	break;
            }
        }
	}

	public void ActionClick(int i) {
		// TODO Auto-generated method stub

	}

	public void update() {
		// TODO Auto-generated method stub

	}

	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		g.drawImage(img[HighScoreScreen.BG], 0, 0, Graphics.TOP|Graphics.LEFT);
		g.drawImage(img[HighScoreScreen.MENU], 0, 240, Graphics.BOTTOM|Graphics.LEFT);
		g.drawImage(img[HighScoreScreen.FORM], 10, 40, Graphics.TOP|Graphics.LEFT);
		g.setColor(0,0,0);
		Statistic temp;
		for(int i = 0; i< v.size(); i++){
			temp = new Statistic((Statistic)v.elementAt(i));
			g.drawString(temp.getName(), 50, 95+i*25, Graphics.TOP|Graphics.LEFT);
			g.drawString(String.valueOf(temp.getWinCount()), 165, 95+i*25, Graphics.TOP|Graphics.LEFT);
			g.drawString(String.valueOf(temp.getLoseCount()), 195, 95+i*25, Graphics.TOP|Graphics.LEFT);
		}
		temp = null;
	}

}
